package com.pansoft.kline.activity;

import android.content.Context;
import android.content.Intent;

import com.efounder.chat.http.JFCommonRequestManager;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.util.AppContext;
import com.efounder.util.ToastUtil;
import com.google.gson.Gson;
import com.pansoft.kline.base.BaseViewPagerActivity;
import com.pansoft.kline.fragment.BourseSecondPageFragment;
import com.pansoft.kline.greendao.DaoSessionUtil;
import com.pansoft.kline.model.BourseSecondPageModel;
import com.pansoft.kline.model.ConfigSiteBean;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.constant.MarketConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : zzj
 * @e-mail : zhangzhijun@pansoft.com
 * @date : 2018/12/3 10:31
 * @desc :  交易所二级页面
 * @version: 1.0
 */
public class BourseActivity extends BaseViewPagerActivity {

    /**
     *  当前交易所的交易币的id
     */
    private String site;
    /**
     *     传入fragment的currency集合
     */
    private List<String> currencyTitle = new ArrayList<>();

    public static void start(Context context,String marketId) {
        Intent starter = new Intent(context, BourseActivity.class);
        starter.putExtra("site",marketId);
        context.startActivity(starter);
    }

    @Override
    protected void initData() {
        site = getIntent().getStringExtra("site");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("currency","ALL");
        JFCommonRequestManager.getInstance(this).requestGetByAsyn(TAG, MarketConstant.MARKET_BOURSE_SECOND_PAGE_URL+site, hashMap, new JFCommonRequestManager.ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                try {
                    BourseSecondPageModel bourseSecondPageModel = new Gson().fromJson(result, BourseSecondPageModel.class);
                    if (bourseSecondPageModel.getCode() != 0) {
                        ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail);
                        return;
                    }
                    disposeBouSecondPageData(bourseSecondPageModel);
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail);
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail_abnormal);
            }
        });
    }

    /**
     * 加载title和fragment
     * @param bourseSecondPageModel
     */
    private void disposeBouSecondPageData(BourseSecondPageModel bourseSecondPageModel) {
        if(bourseSecondPageModel!=null){
            List<String> mTitle = bourseSecondPageModel.getData().getCurrencies();
            if(mTitle!=null&&mTitle.size()!=0){
                mTitles.addAll(mTitle);
                currencyTitle.addAll(mTitle);
                mTitles.remove(0);
                mTitles.add(0,getResources().getString(R.string.kline_detail_bourse_second_all));
            }
        }

        initFragment();
        initAdapter();
        ConfigSiteBean siteBean = DaoSessionUtil.getInstance().getConfigSiteBeanDao().load(site);
        setTitle(siteBean.getTitle());
    }

    /**
     * 初始化fragments
     */
    private void initFragment() {
        if(mTitles.size()!=0){
            for(int i = 0;i<mTitles.size();i++){
                mFragments.add(BourseSecondPageFragment.newInstance(site,currencyTitle.get(i)));
            }
        }

    }

    @Override
    protected List<String> createPageTitle() {
        return mTitles;
    }

    @Override
    protected List<BaseFragment> createFragments() {
        return mFragments;
    }
}
